@extends('shop_admin.admin')

@section('content')
<link rel="stylesheet" href="{{ asset('/js/bootstrap-switch/css/bootstrap-switch.min.css') }}">
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        商品管理
        <small>商品列表</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/shop_admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/shop_admin/goods">商品管理</a></li>
        <li class="active">商品列表</li>
    </ol>
</section>
<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">


            <div class="box box-primary ">
                <div class="box-body table-responsive">

                    {!! Form::model(@$goods,array('route' => 'shop_admin.goods.index','method' => 'get','class'=>'form-search form-inline pull-right')) !!}


                        <?php $select = $category::getTreeList();?>

                        <?php

                        function myspace($n,$hasChild){
                            $enum = ['├','└'];
                            if ($hasChild){
                                $space = $enum[0];
                            }else{
                                $space = $enum[1];
                            }
                            $str = '';
                            for ($i=0;$i<=$n;$i++){
                                $str.='&nbsp;&nbsp;';
                            }
                            return $str.$space;
                        }
                        $seletcItem[0] = '所有商品分类';

                        $disabledOption = [];
                        foreach ($select as $k=> $item){

                            if($item->has_children){
                                $disabledOption[]=$item->cat_id;
                            }
                            $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                        }

                        ?>

                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::selectRequired('cat_id',$seletcItem,@$_GET['cat_id'],['class'=>'form-control select2','id'=>'cat_id','data-placeholder'=>'所有商品分类' ],$disabledOption)!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('brand_id',$brandArr,@$_GET['brand_id'],['class'=>'form-control select2','data-placeholder'=>'所有品牌'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('type',$typeArr,@$_GET['type'],['class'=>'form-control','placeholder'=>'商品类型'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('is_on_sale',$isOnSaleArr,@$_GET['is_on_sale'],['class'=>'form-control','placeholder'=>'上下架'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::select('goods_number',$goodsNumberArr,@$_GET['goods_number'],['class'=>'form-control','placeholder'=>'库存'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::text('goods_name',$search['goods_name'],['class'=>'form-control','placeholder'=>'商品名称'])!!}
                            </div>
                            <div class="form-group" style="margin-right: 15px;">
                                {!! Form::text('goods_sn',$search['goods_sn'],['class'=>'form-control','placeholder'=>'商品编码'])!!}
                            </div>
                            <button type="submit" class="btn btn-primary ">搜索</button>
                            <button type="button" onclick="window.location.href='/shop_admin/goods'" class="btn btn-default">重置</button>

                    {!! Form::close() !!}

                    <a href="{{route('shop_admin.goods.create')}}" class="btn btn-success"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>

                </div>

            </div><!-- end .box box-primary -->


            <div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title">商品列表</h3>
                    <div class="box-tools">
                        <a href="{{route('shop_admin.goods.create')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon-plus"></i> 添加</a>
                    </div>
                </div>
                <!-- /.box-header -->

                {!! Form::model($goods,array('route' => 'shop_admin.goods.sort','class'=>'form-horizontal','method'=>'put')) !!}
                <div class="box-body table-responsive no-padding">


                    <table class="table table-hover">
                        <tr>
                            <!-- <th class="text-center" style="width: 60px">选择</th> -->
                            <th style="width: 50px">ID</th>
                            <th style="width: 50px">货号</th>
                            <th>名称</th>
                            <th style="width: 150px">分类</th>
                            {{--<th style="width: 100px">编号</th>--}}
                            <th style="width: 80px">价格</th>
                            <th style="width: 80px">库存</th>
                            <th style="width: 80px;display:none;">首页推荐</th>
                            {{--<th style="width: 80px">位置推荐</th>--}}

                            <th style="width: 80px">类型</th>
                            <th style="width: 80px">审核状态</th>
                            <th style="width: 80px">上下架</th>
                            <th style="width: 300px;">操作</th>

                        </tr>

                        <tr style="display:none;">
                            <td colspan="11">
                            {!! Form::open(['route' => ['shop_admin.goods.destroy'],'name'=>'hid', 'method'=>'delete']) !!}

                            {!! Form::submit('删除',array('class' => 'btn btn-danger')) !!}

                            {!!Form::close()   !!}
                            </td>
                        </tr>
                        @foreach ($goods as $item)
                        <tr>
                            <!-- @if($item->is_on_sale==1)
                            <td class="text-center">{!! Form::checkbox('selGoods', $item->goods_id, false,['id'=>$item->goods_id]) !!}</td>
                            @else
                            <td>{!! Form::checkbox('selGoods', $item->goods_id, false,['id'=>$item->goods_id,'style'=>'width:40px; text-align:center','disabled'=>'disabled']) !!}</td>
                            @endif -->
                            <td>{{$item->goods_id}}</td>
                            <td>{{$item->goods_sn}}</td>
                            <td><a href="{{route('front.goods.detail', $item->goods_id)}}?proview=1" target="_blank">{{$item->goods_name}}</a></td>
                            <td>{{$item->cat_name}}</td>
                            {{--<td>{{$item->goods_sn}}</td>--}}
                            <td>{{$item->shop_price}}</td>
                            <td>{{$item->goods_number}}</td>
                            <td style="display:none;">
                                    @if($item->is_home_recommend==1)
                                    <i class="is_home_recommend glyphicon glyphicon-check" style=" cursor: pointer;color: green;" goods_id="{{$item->goods_id}}" name='is_home_recommend'></i>
                                    @elseif($item->is_home_recommend==0)
                                    <i class="is_home_recommend glyphicon glyphicon-unchecked" style=" cursor: pointer;color: red;" goods_id="{{$item->goods_id}}" name='is_home_recommend'></i>
                                    @endif
                            </td>
                            <td>
                                @if($item->type==0)
                                    一般商品
                                @elseif($item->type==1)
                                    e贸易商品
                                @endif
                            </td>
                            <td width="6%" style="text-align: center" class="audit_status " name="audit_status" id="{{$item->id}}" audit_status="{{$item->audit_status}}">
                                <?php
                                    // $auditArr = ['0'=>'未审核', '1'=>'审核通过', '2'=>'审核通过'];
                                    switch ($item->audit_status) {
                                        case '0':
                                            echo '未审核';
                                            break;

                                        case '1':
                                            echo '审核通过';
                                            break;

                                        case '2':
                                            echo '审核通过';
                                            break;
                                    }
                                ?>

                            </td>
                            <td>
                                @if($item->is_on_sale==1)
                                <input type="checkbox" class="J-switch" checked @if($item->audit_status!=1) disabled @endif  goods_id="{{$item->goods_id}}">
                                {{--<i class="toggle_sale glyphicon glyphicon-check @if($item->audit_status!=1) disabled @endif" style=" @if($item->audit_status!=1) color: gray @else cursor: pointer;color: green @endif"  goods_id="{{$item->goods_id}}"></i>--}}
                                @elseif($item->is_on_sale==2)
                                <input type="checkbox" class="J-switch" @if($item->audit_status!=1) disabled @endif  goods_id="{{$item->goods_id}}">
                                {{--<i class="toggle_sale glyphicon glyphicon-unchecked red @if($item->audit_status!=1) disabled @endif" style=" @if($item->audit_status!=1) color: gray @else cursor: pointer;color: red @endif;" goods_id="{{$item->goods_id}}"></i>--}}
                                @endif
                            </td>
                            <td>
                                <a href="{{Route('shop_admin.goods.edit',$item->goods_id)}}?back_url=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" type="button" class="btn  btn-default btn-xs"><i class="icon  glyphicon glyphicon-edit"></i> 修改</a>
                                <button type="button" class="btn  btn-danger btn-xs"  data-toggle="modal" data-target="#modal{{$item->goods_id}}"><i class="icon  glyphicon glyphicon-remove"></i> 删除</button>

                                <div id="modal{{$item->goods_id}}" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">删除警告:</h4>
                                            </div>
                                            <div class="modal-body">
                                                <p>确认要删除[{{$item->goods_name}}]吗?</p>
                                            </div>
                                            <div class="modal-footer">


                                                {!! Form::open(['route' => ['shop_admin.goods.destroy', $item->goods_id],'name'=>$item->goods_id, 'method'=>'delete']) !!}

                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>

                                                {!! Form::submit('删除',array('class' => 'btn btn-danger')) !!}

                                                {!!Form::close()   !!}
                                            </div>
                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->

                                <button type="button" class="btn  btn-success btn-xs"  data-toggle="modal" data-target="#modal{{trim($item->goods_id)}}plus"><i class="icon  glyphicon glyphicon-plus"></i> 入库</button>
                                <button type="button" class="btn  btn-primary btn-xs"  data-toggle="modal" data-target="#modal{{trim($item->goods_id)}}reduce"><i class="icon  glyphicon glyphicon-minus"></i> 出库</button>

                                <div id="modal{{trim($item->goods_id)}}plus" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">

                                            <input type="hidden" name="pre_url" value="<?php if(@$_SERVER['HTTP_REFERER']==''){ ?>{{route('shop_admin.goods.index')}}<?php }else{echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}?>"/>

                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">入库操作:</h4>
                                            </div>

                                            <div class="modal-body" style="height:80px;">

                                                <div class="form-group col-xs-5 @if($errors->has('goods_stock')) has-error   @endif" style="margin-right: 15px;">
                                                    {{--<label for="exampleInputPassword1" class="col-sm-2 control-label">数量：</label>--}}
                                                    {!! Form::text('goods_stock', '',['class'=>'form-control','placeholder'=>'数量','required'=>'required','id'=>'goods_stock_plus_'.$item->goods_id])!!}
                                                    <div>
                                                        @if($errors->has('goods_stock'))
                                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('goods_stock')}}</label>
                                                        @endif
                                                    </div>
                                                </div>

                                                {!! Form::hidden('operation', 'plus') !!}
                                            </div>

                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <button type="button" class="btn btn-danger J-updateStock" data-dismiss="modal" data-url="<?php echo route('admin.goods.updateStock', $item->goods_id);?>" data-operation="plus" data-goods_id="{{$item->goods_id}}" data-pre_url="<?php if(@$_SERVER['HTTP_REFERER']==''){ ?>{{route('admin.goods.index')}}<?php }else{echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}?>" >确认</button>

                                            </div>

                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->
                                <div id="modal{{trim($item->goods_id)}}reduce" class="modal fade">
                                    <div class="modal-dialog">
                                        <div class="modal-content">

                                            <input type="hidden" name="pre_url" value="<?php if(@$_SERVER['HTTP_REFERER']==''){ ?>{{route('shop_admin.goods.index')}}<?php }else{echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}?>"/>

                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title">出库操作:</h4>
                                            </div>

                                            <div class="modal-body" style="height:70px;">

                                                <div class="form-group col-xs-5 @if($errors->has('goods_stock')) has-error   @endif" style="margin-right: 15px;">
                                                    {{--<label for="exampleInputPassword1" class="col-sm-2 control-label">数量：</label>--}}
                                                    {!! Form::text('goods_stock', '',['class'=>'form-control','placeholder'=>'数量','required'=>'required','id'=>'goods_stock_reduce_'.$item->goods_id])!!}
                                                    <div>
                                                        @if($errors->has('goods_stock'))
                                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('goods_stock')}}</label>
                                                        @endif
                                                    </div>
                                                </div>

                                                {!! Form::hidden('operation', 'reduce') !!}
                                            </div>

                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                                <button type="button" class="btn btn-danger J-updateStock" data-dismiss="modal" data-url="<?php echo route('admin.goods.updateStock', $item->goods_id);?>" data-operation="reduce" data-goods_id="{{$item->goods_id}}" data-pre_url="<?php if(@$_SERVER['HTTP_REFERER']==''){ ?>{{route('admin.goods.index')}}<?php }else{echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}?>" >确认</button>

                                            </div>

                                        </div><!-- /.modal-content -->
                                    </div><!-- /.modal-dialog -->
                                </div><!-- /.modal -->

                            </td>
                        </tr>
                        @endforeach

                    </table>

                </div>
                <!-- /.box-body -->

                <div class="box-footer">
                    <!-- 分页 -->
                    <div class="pull-right no-margin-pager">
                        {!! $goods->appends(['cat_id' => $search['cat_id'], 'brand_id' => $search['brand_id'], 'goods_name' => $search['goods_name'], 'goods_sn' => $search['goods_sn'], 'type' => $search['type'], 'is_on_sale' => $search['is_on_sale'],'goods_number' => $search['goods_number']])->render() !!}
                    </div>

                </div>

                {!! Form::hidden('_method', 'PUT') !!}


                {!! Form::close()  !!}
            </div>
            <!-- /.box -->

        </div>
    </div>


<!-- Modal -->
<div class="modal fade" id="warmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close close_btn" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">信息提示</h4>
      </div>
      <div class="modal-body" id="errorMessage">

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
</div>


</section>
<!-- /.content -->
<!-- Select2 -->
<script type="text/javascript" src="{{ asset("/components/AdminLTE/plugins/select2/select2.full.min.js") }}"></script>
<script type="text/javascript" src="{{ asset("/js/bootstrap-switch/bootstrap-switch.min.js") }}"></script>

<script>
    //初始化开关插件-上下架  shenchangwei  2016-03-15
    $('.J-switch').bootstrapSwitch({
        size: 'mini',
        onColor: "primary",
        offColor: "warning",
        onText: "上架",
        offText: "下架",
        onInit: function() {}
    }).on('switchChange.bootstrapSwitch', function(event, state) {
//        console.log(this); // DOM element
//        console.log(event); // jQuery event
//        console.log(state); // true | false
        var that=$(this),checkbox=that.closest('tr').find('[type="checkbox"]');
        var id = that.attr('goods_id');

        $.ajax({
            type: "GET",
            url: '/shop_admin/goods/update_one/'+id,
            data:'',
            dataType: "json",
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) {
                if(msg.is_on_sale==1){
                    checkbox.prop('disabled',false);
                }else{
                    checkbox.prop('disabled',true);
                }
            }
        }); //end ajax

    });

//Initialize Select2 Elements
$(".select2").select2();

var wind_status = '';
$(".close_btn").click(function(e){
    wind_status = 'close';
});


//TODO 备份，测试功能无误后删除
$(".toggle_sale").click(function(e){
    var that=$(this),checkbox=that.closest('tr').find('[type="checkbox"]');
    var id = $(e.currentTarget).attr('goods_id');

    if(that.hasClass('disabled')) return false;
     $.ajax({ 
            type: "GET", 
            url: '/shop_admin/goods/update_one/'+id, 
            data:'',
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 

                    if(msg.is_on_sale==1){

                        that.removeClass('glyphicon-unchecked');
                        that.css("color","green");
                        that.addClass('glyphicon-check');
                        checkbox.prop('disabled',false);
                    }else{
                        that.css("color","red");

                        that.removeClass('glyphicon-check');

                        that.addClass('glyphicon-unchecked');
                        checkbox.prop('disabled',true);

                    }
            
            }
        }); //end ajax


});

var goodsArr =[]; 
$('#addAct').click(function(){
    goodsArr =[]; 

    $('input[name="selGoods"]:checked').each(function(){ 
        goodsArr.push($(this).val()); 
    });

    if(goodsArr.length==0){

        $('#warmModal').modal('show');
        $('#errorMessage').html('您还没有选择任何商品');

    }else{

        $('#actCat').modal('show');

    }//end if()
    
});

$('#addRecommend').click(function(){
    goodsArr =[]; 

    $('input[name="selGoods"]:checked').each(function(){ 
        goodsArr.push($(this).val()); 
    });

    if(goodsArr.length==0){

        $('#warmModal').modal('show');
        $('#errorMessage').html('您还没有选择任何商品');

    }else{

        $('#recommend').modal('show');

    }//end if()
    
});



$('#activity_id').click(function(){
    
    var activity_id = $("#activity_id option:selected").val();
    $("#activity_id").css("color","black");
    $("#act_cat_id").css("color","black");

    urls = "/shop_admin/activity_cat/"+activity_id;
    
    $.ajax({ 
        type: "GET", 
        url: urls, 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 
            $('#act_cat_id').empty();
            
            if(msg.length==0){

                $('#act_cat_id').append('<option value="0">请选择分类</option>');
            }else{

                $.each(msg, function(n, value){
                    $('#act_cat_id').append('<option value="'+n+'">'+value+'</option>');
                });
            }//end if()
                   
        }
    }); //end ajax

});


$('#add_goods_to_activity').click(function(e){
    var dataForm = {};
    var activity_id = $('#activity_id').val();
    var act_cat_id = $("#act_cat_id").val();

    wind_status = 'add_activity';

    if(activity_id==null || activity_id=='' || activity_id==0 || act_cat_id==null || act_cat_id=='' || act_cat_id==0){


    }else{

        dataForm.goodsArr = goodsArr;

        urls = "/shop_admin/addGoodsToAct/"+act_cat_id;
        
        $.ajax({ 
            type: "GET", 
            url: urls, 
            data: dataForm,
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 
                
                console.log(msg);
                if(msg.status){
                    
                    $('#warmModal').modal('show');
                    $('#errorMessage').html('添加成功');


                }//end if()
                       
            }
        }); //end ajax
    }//end if()

});
   

$('#actCat').on('hidden.bs.modal', function () {

    var activity_id = $('#activity_id').val();
    var act_cat_id = $("#act_cat_id").val();

    if(wind_status == 'add_activity'){

        if(activity_id==null || activity_id=='' || activity_id==0){

            $("#activity_id").css("color","red");

            $("#activity_id option").each(function(){ //遍历全部option
                $(this).css("color","black");
                $("#activity_id option[value='0']").css("color","red");
            });

            $('#actCat').modal('show');

        }//end if()

        if(act_cat_id==null || act_cat_id=='' || act_cat_id==0){
            $("#act_cat_id").css("color","red");

            $("#activity_id option").each(function(){ //遍历全部option
                $(this).css("color","black");
                $("#activity_id option[value='0']").css("color","red");
            });

            $('#actCat').modal('show');
        }//end if()
    }//end if()
});


$(".is_home_recommend").click(function(e){

    var id = $(e.currentTarget).attr('goods_id');
    var is_home_recommend = $(e.currentTarget).attr('name');

    $.ajax({ 
        type: "GET", 
        url: '/shop_admin/goods/'+id+'/'+is_home_recommend+'/update_two', 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 

                if(msg.is_home_recommend==1){

                    $(e.currentTarget).removeClass('glyphicon-unchecked');
                    $(e.currentTarget).css('color', 'green');

                    $(e.currentTarget).addClass('glyphicon-check');
                    
                }else{

                    $(e.currentTarget).removeClass('glyphicon-check');
                    $(e.currentTarget).css('color', 'red');

                    $(e.currentTarget).addClass('glyphicon-unchecked');

                }
        
        }
    }); //end ajax


});

$(".is_cat_recommend").click(function(e){

    var id = $(e.currentTarget).attr('goods_id');
    var is_cat_recommend = $(e.currentTarget).attr('name');

    $.ajax({ 
        type: "GET", 
        url: '/shop_admin/goods/'+id+'/'+is_cat_recommend+'/update_two', 
        data:'',
        dataType: "json", 
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        async:true,
        cache:false,
        success: function (msg) { 

                if(msg.is_cat_recommend==1){

                    $(e.currentTarget).removeClass('glyphicon-unchecked');
                    $(e.currentTarget).css('color', 'green');

                    $(e.currentTarget).addClass('glyphicon-check');
                    
                }else{

                    $(e.currentTarget).removeClass('glyphicon-check');
                    $(e.currentTarget).css('color', 'red');

                    $(e.currentTarget).addClass('glyphicon-unchecked');

                }
        
        }
    }); //end ajax


});



$('#add_goods_to_recommend').click(function(e){

    var dataForm = {};
    var recommend_id = $('#recommend_id').val();
    wind_status = 'add_recommend';

    if(recommend_id==null || recommend_id=='' || recommend_id==0 ){


        $("#recommend_id").css("color","red");

        $("#recommend_id option").each(function(){ //遍历全部option
            $(this).css("color","black");
            $("#recommend_id option[value='0']").css("color","red");
        });

        //$('#recommend').modal('show');

    }else{
        wind_status = 'close';

        dataForm.relationArr = goodsArr;

        urls = "/shop_admin/add_goods_to_recommend/"+recommend_id;

        $.ajax({ 
            type: "GET", 
            url: urls, 
            data: dataForm,
            dataType: "json", 
            headers: {
                'X-CSRF-Token': "{{csrf_token()}}"
            },
            async:true,
            cache:false,
            success: function (msg) { 
                
                console.log(msg);
                if(msg.status){
                    
                    $('#warmModal').modal('show');
                    $('#errorMessage').html('添加成功');

                }//end if()
                       
            }
        }); //end ajax
    }//end if()
});


$('#recommend').on('hidden.bs.modal', function () {

    if(wind_status == 'add_recommend'){
        $('#recommend').modal('show');
    }//end if()
});


$('#recommend_id').click(function(){
        $("#recommend_id").css("color","black");
});

var auditSel=$('.js-audit');
auditSel.on('change selected',function(){
    var that=$(this),val=that.val();
    var id = that.data('id');
//    var sibOnSale=that.closest('td').next('td').find('i.toggle_sale');
    var sibOnSale=that.closest('td').next('td').find('.J-switch');


    $.ajax({
        url:'/shop_admin/goods/toggleShow',
        type:'post',
        dataType:'json',
        data:{'audit_status':val,'id':id},
        success:function(res){
            console.log(val!=1);
            sibOnSale.bootstrapSwitch('disabled',val!=1);
//            if(val!=1){
//                  sibOnSale.addClass('glyphicon-unchecked');
//                  sibOnSale.addClass('disabled').css({'color':'gray','cursor':'auto'});
//            }else{
//                var color='';
//                if(sibOnSale.hasClass('glyphicon-check')){
//                color='green';
//                }else{
//                color='red';
//                }
//              sibOnSale.removeClass('disabled').css({'color':color,'cursor':'pointer'});
//            }

        },
        error:function(e){

        }
    });




});


$('.J-updateStock').on('click',function(e){

    var that=$(this),goods_id = that.data('goods_id'),operation = that.data('operation'),goods_stock_num = $('#goods_stock_'+operation+'_'+goods_id).val(),url = that.data('url'),pre_url=that.data('pre_url');

    var dataObj = {};

    dataObj.goods_stock = goods_stock_num;
    dataObj.operation = operation;
    dataObj.pre_url = pre_url;

    $.ajax({
        type: "GET",
        url: url,
        data: dataObj,
        dataType: "json",
        headers: {
            'X-CSRF-Token': "{{csrf_token()}}"
        },
        success: function (msg) {
            console.log(msg);
            $('#warmModal').modal('show');
            $('#modal'+goods_id+operation).modal('hide');

            if(msg.status){

                var mesg = '成功';
                if (operation=="reduce") { mesg = '出库'+mesg; }
                if (operation=="plus") { mesg = '入库'+mesg; }
                $('#errorMessage').html(mesg);
                location.reload();

            }else{

                $('#errorMessage').html(msg.message);
            }//end if()

        }
    }); //end ajax

});

</script>
@endsection